<!DOCTYPE html>
<html>
<head>
  <script src="../../processing.js"></script>
  <script src="../init.js"></script>
<link rel="stylesheet" href="../style.css"/></head>
<body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
<h2>LinearImage</h2>

<p>This code was updated from the Java source to work with Processing.js asynchronous image loading.</p>

<p>Click and drag mouse up and down to control the signal. 
Press and hold any key to watch the scanning.</p>

<p><a href="http://processing.org/learning/topics/linearimage.html"><b>Original Processing.org Example:</b> LinearImage</a><br>
<script type="application/processing">
PImage a;
boolean onetime = true;
int[] aPixels = new int[200*200];
int direction = 1;

float signal;

void setup() 
{
  size(33, 33);
  stroke(255);
  a = loadImage("data/cait.jpg", null, function(){
    for(int i=0; i<width*height; i++) {
      aPixels[i] = a.pixels[i];
    }
  });  
  frameRate(30);
}

void draw() 
{
  if (signal > width-1 || signal < 0) { 
    direction = direction * -1; 
  }

  if(mousePressed) {
    signal = abs(mouseY%height);
  } else {
    signal += (0.3*direction);  
  }
  
  
  if(keyPressed) {
    image(a, 0, 0);
    /*
    loadPixels();
    for (int i=0; i<width*height; i++) { 
      pixels[i] = aPixels[i];  
    }
    updatePixels();
    */
    line(0, signal, width, signal);
  } else {
    loadPixels();
    for (int i=0; i<width*height; i++) { 
      pixels[i] = aPixels[int((width*int(signal))+(i%width))];
    }
    updatePixels();
  }
  
}
</script><canvas width="200" height="200"></canvas></p>
<div style="height:0px;width:0px;overflow:hidden;"><img src='data/cait.jpg' id='cait.jpg'/></div>

<pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
// unless otherwise stated.</b>
PImage a;
boolean onetime = true;
int[] aPixels = new int[200*200];
int direction = 1;

float signal;

void setup() 
{
  size(33, 33);
  stroke(255);
  a = loadImage("data/cait.jpg", null, function(){
    for(int i=0; i<width*height; i++) {
      aPixels[i] = a.pixels[i];
    }
  });  
  frameRate(30);
}

void draw() 
{
  if (signal &gt; width-1 || signal &lt; 0) { 
    direction = direction * -1; 
  }

  if(mousePressed) {
    signal = abs(mouseY%height);
  } else {
    signal += (0.3*direction);  
  }
  
  
  if(keyPressed) {
    loadPixels();
    for (int i=0; i&lt;width*height; i++) { 
      pixels[i] = aPixels[i];  
    }
    updatePixels();
    line(0, signal, width, signal);
  } else {
    loadPixels();
    for (int i=0; i&lt;width*height; i++) { 
      pixels[i] = aPixels[int((width*int(signal))+(i%width))];
    }
    updatePixels();
  }
  
}</pre>
</body>
</html>
